/*
 *   Copyright 2011 Google Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.example.android.training.optimizinglayouts;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class OptimizedListAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater;
	private static final int NUM_ITEMS = 500;
	private SlowImageLoader mFakeImageLoader;
	private String ITEM_PRE_TEXT;
	private String RENDER_TIME_PRE_TEXT;
	private String MILLISECONDS_TEXT;
	private long START_TIMESTAMP;

	public OptimizedListAdapter(Context cxt) {
		// Cache the LayoutInflate to avoid asking for a new one each time.
		mLayoutInflater = LayoutInflater.from(cxt);
		mFakeImageLoader = new SlowImageLoader(cxt);

		ITEM_PRE_TEXT = cxt.getString(R.string.item_pre_text);
		RENDER_TIME_PRE_TEXT = cxt.getString(R.string.render_time_text);
		MILLISECONDS_TEXT = cxt.getString(R.string.milliseconds);
		START_TIMESTAMP = System.currentTimeMillis();
	}

	@Override
	public int getCount() {
		return NUM_ITEMS;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// A ViewHolder keeps references to children views to avoid unneccessary
		// calls
		// to findViewById() on each row.
		ViewHolder holder;

		// When convertView is not null, we can reuse it, there is no need
		// to reinflate it. We only inflate a new View when the convertView
		// supplied by ListView is null.
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.listitem, null);

			// Creates a ViewHolder and store references to the child
			// views we want to bind data to.
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
			holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
			holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
			holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// holder.icon.setImageDrawable(drawable);
		holder.text.setText(new StringBuffer(ITEM_PRE_TEXT).append(position));
		holder.timestamp.setText(new StringBuffer(RENDER_TIME_PRE_TEXT).append(System.currentTimeMillis()
				- START_TIMESTAMP).append(MILLISECONDS_TEXT));
		holder.position = position;
		holder.progress.setVisibility(View.VISIBLE);
		holder.icon.setVisibility(View.GONE);

		// Using an AsyncTask to load the slow images in a background thread
		new AsyncTask<ViewHolder, Void, Bitmap>() {
			private ViewHolder v;

			@Override
			protected Bitmap doInBackground(ViewHolder... params) {
				v = params[0];
				return mFakeImageLoader.getImage();
			}

			@Override
			protected void onPostExecute(Bitmap result) {
				super.onPostExecute(result);
				if (v.position == position) {
					// If this item hasn't been recycled already, hide the
					// progress and set and show the image
					v.progress.setVisibility(View.GONE);
					v.icon.setVisibility(View.VISIBLE);
					v.icon.setImageBitmap(result);
				}
			}

		}.execute(holder);

		return convertView;
	}

	static class ViewHolder {
		TextView text;
		TextView timestamp;
		ImageView icon;
		ProgressBar progress;
		int position;
	}
	
	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

}
